package dao;

import model.Product;
import util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品数据访问对象类，用于操作商品表数据
 */
public class ProductDAO {
    /**
     * 添加商品
     * @param product 要添加的商品对象
     */
    public void addProduct(Product product) {
        String sql = "INSERT INTO product(product_name, price, category, quantity) VALUES(?,?,?,?)";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, product.getProductName());
            preparedStatement.setDouble(2, product.getPrice());
            preparedStatement.setString(3, product.getCategory());
            preparedStatement.setInt(4, product.getQuantity());

            int rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("商品添加成功！");
            } else {
                System.out.println("商品添加失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据商品 ID 获取商品
     * @param productId 商品 ID
     * @return 对应的商品对象，若未找到则返回 null
     */
    public Product getProductById(int productId) {
        String sql = "SELECT * FROM product WHERE product_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, productId);

            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                Product product = new Product();
                product.setProductId(resultSet.getInt("product_id"));
                product.setProductName(resultSet.getString("product_name"));
                product.setPrice(resultSet.getDouble("price"));
                product.setCategory(resultSet.getString("category"));
                product.setQuantity(resultSet.getInt("quantity"));

                return product;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取所有商品
     * @return 商品列表
     */
    public List<Product> getAllProducts() {
        List<Product> productList = new ArrayList<>();
        String sql = "SELECT * FROM product";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            while (resultSet.next()) {
                Product product = new Product();
                product.setProductId(resultSet.getInt("product_id"));
                product.setProductName(resultSet.getString("product_name"));
                product.setPrice(resultSet.getDouble("price"));
                product.setCategory(resultSet.getString("category"));
                product.setQuantity(resultSet.getInt("quantity"));

                productList.add(product);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return productList;
    }

    /**
     * 更新商品
     * @param product 要更新的商品对象
     */
    public void updateProduct(Product product) {
        String sql = "UPDATE product SET product_name = ?, price = ?, category = ?, quantity = ? WHERE product_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, product.getProductName());
            preparedStatement.setDouble(2, product.getPrice());
            preparedStatement.setString(3, product.getCategory());
            preparedStatement.setInt(4, product.getQuantity());
            preparedStatement.setInt(5, product.getProductId());

            int rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("商品更新成功！");
            } else {
                System.out.println("商品更新失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 在 ProductDAO.java 中添加以下方法
    /**
     * 删除商品
     * @param productId 商品 ID
     */
    public void deleteProduct(int productId) {
        String sql = "DELETE FROM product WHERE product_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, productId);

            int rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("商品删除成功！");
            } else {
                System.out.println("商品删除失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}